package com.ruoyi.web.controller.source;


import cn.hxzy.common.OperateType;
import cn.hxzy.common.SourceStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.order.service.TtWaybillService;
import com.hxzy.site.entity.TtSite;
import com.hxzy.site.service.TtSiteService;
import com.hxzy.source.entity.TtSourceList;
import com.hxzy.source.entity.TtSourceOperation;
import com.hxzy.source.entity.vo.SourceVo;
import com.hxzy.source.service.TtSourceListService;
import com.hxzy.source.service.TtSourceOperationService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-26
 */
@RestController
@RequestMapping("/ttSourceList")
@DataSource(DataSourceType.SOURCE)
public class TtSourceListController {


    @Autowired
    private TtSourceListService ttSourceListService;

    @Autowired
    private TtSiteService siteService;

    @Autowired
    private TtWaybillService ttWaybillService;

    @Autowired
    private TtSourceOperationService ttSourceOperationService;

    @GetMapping("/getSourceList/{pageIndex}/{pageSize}")
    @Anonymous

    public AjaxResult getSourceList(@PathVariable Integer pageIndex,@PathVariable Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<TtSourceList> ttSourceLists = ttSourceListService.list();
        List<TtSite> ttSites = siteService.getList();
        ttSourceLists.stream().forEach(item1->{
            item1.setSourceStatusName(SourceStatus.getSourceStatusValue(item1.getSourceStatus()));
            ttSites.stream().forEach(item->{
                if (Objects.equals(item1.getUnloadingSiteId(),item.getId())){
                    item1.setUnloadingSiteId(item.getName());
                }
                if (Objects.equals(item1.getLoadingSiteId(),item.getId())){
                    item1.setLoadingSiteId(item.getName());
                }
            });
        });
        PageInfo<TtSourceList>pageInfo=new PageInfo<>(ttSourceLists);
        return AjaxResult.success(pageInfo.getList()).put("total",pageInfo.getTotal());
    }


    @PostMapping("/searchSource/{pageIndex}/{pageSize}")
    public AjaxResult searchSource(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,@RequestBody TtSourceList ttSourceList){

        List<TtSite> ttSites = siteService.getList();
        PageHelper.startPage(pageIndex,pageSize);
        List<TtSourceList> ttSourceLists = ttSourceListService.searchSource(ttSourceList);

        ttSourceLists.stream().forEach(item1->{
            item1.setSourceStatusName(SourceStatus.getSourceStatusValue(item1.getSourceStatus()));
            ttSites.stream().forEach(item->{
                if (Objects.equals(item1.getUnloadingSiteId(),item.getId())){
                    item1.setUnloadingSiteId(item.getName());
                }
                if (Objects.equals(item1.getLoadingSiteId(),item.getId())){
                    item1.setLoadingSiteId(item.getName());
                }
            });
        });
        PageInfo<TtSourceList>pageInfo=new PageInfo<>(ttSourceLists);
        return AjaxResult.success(pageInfo.getList()).put("total",pageInfo.getTotal());
    }

    @GetMapping("/getSourceByStatus/{pageIndex}/{pageSize}/{status}")
    public AjaxResult getSourceByStatus(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,@PathVariable Integer status){
        PageHelper.startPage(pageIndex,pageSize);
        QueryWrapper<TtSourceList>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("source_status",status);
        List<TtSourceList> list1 = ttSourceListService.list(queryWrapper);
        List<TtSite> list = siteService.getList();
        list1.stream().forEach(item1->{
            item1.setSourceStatusName(SourceStatus.getSourceStatusValue(item1.getSourceStatus()));
            list.stream().forEach(item->{
                if (Objects.equals(item1.getUnloadingSiteId(),item.getId())){
                    item1.setUnloadingSiteId(item.getName());
                }
                if (Objects.equals(item1.getLoadingSiteId(),item.getId())){
                    item1.setLoadingSiteId(item.getName());
                }
            });
        });
        PageInfo<TtSourceList> pageInfo=new PageInfo(list1);
        return AjaxResult.success(list1).put("total",pageInfo.getTotal());
    }


    @GetMapping("getSourceInfo/{id}")
    @Anonymous
    public AjaxResult getSourceInfo(@PathVariable String id){
        SourceVo sourceVo=new SourceVo();
        TtSourceList byId = ttSourceListService.getById(id);
        byId.setSourceStatusName(SourceStatus.getSourceStatusValue(byId.getSourceStatus()));
        BeanUtils.copyProperties(byId,sourceVo);
        TtSite sourceOne = siteService.getSourceOne(sourceVo.getLoadingSiteId());
        sourceVo.setLoadingAddress(sourceOne.getLocation());
        sourceVo.setLoadingSiteId(sourceOne.getName());
        TtSite sourceOne1 = siteService.getSourceOne(sourceVo.getUnloadingSiteId());
        sourceVo.setUnloadingAddress(sourceOne1.getLocation());
        sourceVo.setUnloadingSiteId(sourceOne1.getName());
        /*待装车*/
        Long countByStatus = ttWaybillService.getCountByStatus(id,103);
        sourceVo.setToBeLoadedCount(countByStatus);
        /*待卸车*/
        Long countByStatus1 = ttWaybillService.getCountByStatus(id,104);
        sourceVo.setToBeUnloaded(countByStatus1);
        /*已卸车*/
        Long countByStatus2 = ttWaybillService.getCountByStatus(id,106);
        sourceVo.setCompletedCount(countByStatus2);
        /*已取消*/
        Long countByStatus3 = ttWaybillService.getCountByStatus(id,105);
        sourceVo.setCanceledCount(countByStatus3);
        /*已装车*/
        Long countByStatus4 = ttWaybillService.getCountByStatus(id,107);
        Long paymentStatusCount = ttWaybillService.getPaymentStatusCount(id, 101);
        sourceVo.setPaidCount(paymentStatusCount);
        sourceVo.setLoadedCount(countByStatus4);
        List<TtSourceOperation> list = ttSourceOperationService.getList(id);
        list.stream().forEach(item->{
            item.setOperationTypeName(OperateType.geOperateTypeValue(item.getOperationType()));
        });
        sourceVo.setOperateList(list);
        return AjaxResult.success(sourceVo);

    }
}

